package interviewSearch

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/interviewSearch"
	interviewSearchReq "github.com/flipped-aurora/gin-vue-admin/server/model/interviewSearch/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"github.com/olivere/elastic/v7"
	"go.uber.org/zap"
)

type InterviewSearchApi struct {
}

var interviewSearchsService = service.ServiceGroupApp.InterviewSearchServiceGroup.InterviewSearchService

// CreateInterviewSearch 创建interviewSearch表
// @Tags InterviewSearch
// @Summary 创建interviewSearch表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body interviewSearch.InterviewSearch true "创建interviewSearch表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /interviewSearchs/createInterviewSearch [post]
func (interviewSearchsApi *InterviewSearchApi) CreateInterviewSearch(c *gin.Context) {
	var interviewSearchs interviewSearch.InterviewSearch
	err := c.ShouldBindJSON(&interviewSearchs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewSearchsService.CreateInterviewSearch(&interviewSearchs); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteInterviewSearch 删除interviewSearch表
// @Tags InterviewSearch
// @Summary 删除interviewSearch表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body interviewSearch.InterviewSearch true "删除interviewSearch表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /interviewSearchs/deleteInterviewSearch [delete]
func (interviewSearchsApi *InterviewSearchApi) DeleteInterviewSearch(c *gin.Context) {
	var interviewSearchs interviewSearch.InterviewSearch
	err := c.ShouldBindJSON(&interviewSearchs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewSearchsService.DeleteInterviewSearch(interviewSearchs); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteInterviewSearchByIds 批量删除interviewSearch表
// @Tags InterviewSearch
// @Summary 批量删除interviewSearch表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除interviewSearch表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /interviewSearchs/deleteInterviewSearchByIds [delete]
func (interviewSearchsApi *InterviewSearchApi) DeleteInterviewSearchByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewSearchsService.DeleteInterviewSearchByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateInterviewSearch 更新interviewSearch表
// @Tags InterviewSearch
// @Summary 更新interviewSearch表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body interviewSearch.InterviewSearch true "更新interviewSearch表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /interviewSearchs/updateInterviewSearch [put]
func (interviewSearchsApi *InterviewSearchApi) UpdateInterviewSearch(c *gin.Context) {
	var interviewSearchs interviewSearch.InterviewSearch
	err := c.ShouldBindJSON(&interviewSearchs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewSearchsService.UpdateInterviewSearch(interviewSearchs); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindInterviewSearch 用id查询interviewSearch表
// @Tags InterviewSearch
// @Summary 用id查询interviewSearch表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query interviewSearch.InterviewSearch true "用id查询interviewSearch表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /interviewSearchs/findInterviewSearch [get]
func (interviewSearchsApi *InterviewSearchApi) FindInterviewSearch(c *gin.Context) {
	var interviewSearchs interviewSearch.InterviewSearch
	err := c.ShouldBindQuery(&interviewSearchs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reinterviewSearchs, err := interviewSearchsService.GetInterviewSearch(interviewSearchs.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reinterviewSearchs": reinterviewSearchs}, c)
	}
}

// GetInterviewSearchList 分页获取interviewSearch表列表
// @Tags InterviewSearch
// @Summary 分页获取interviewSearch表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query interviewSearchReq.InterviewSearchSearch true "分页获取interviewSearch表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /interviewSearchs/getInterviewSearchList [get]

func InterviewSearch(info interviewSearchReq.InterviewSearchSearch) (list []interviewSearch.InterviewSearchRes, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db

	es := global.EsClient.Search().Index("interview")
	var titleQuery *elastic.MultiMatchQuery
	var levelQuery1 *elastic.MultiMatchQuery
	var levelQuery2 *elastic.MultiMatchQuery
	var levelQuery3 *elastic.MultiMatchQuery
	var companyQuery *elastic.MultiMatchQuery
	var tagQuery *elastic.MultiMatchQuery
	var creatTimeQuery *elastic.RangeQuery

	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		creatTimeQuery = elastic.NewRangeQuery("created_at").Gt(info.StartCreatedAt).Lt(info.EndCreatedAt)
		es = es.Query(creatTimeQuery)
	}
	if info.InterviewTitle != "" {
		fmt.Println("11111")
		titleQuery = elastic.NewMultiMatchQuery(info.InterviewSearch.InterviewTitle, "interview_title")
		es = es.Query(titleQuery)
	}
	if info.Level1 != "" {
		levelQuery1 = elastic.NewMultiMatchQuery(info.InterviewSearch.Level1, "level1_name")
		es = es.Query(levelQuery1)
	}
	if info.Level2 != "" {
		levelQuery2 = elastic.NewMultiMatchQuery(info.InterviewSearch.Level2, "level2_name")
		es = es.Query(levelQuery2)
	}
	if info.Level3 != "" {
		levelQuery3 = elastic.NewMultiMatchQuery(info.InterviewSearch.Level3, "level3_name")
		es = es.Query(levelQuery3)
	}
	if info.CompanyName != "" {
		companyQuery = elastic.NewMultiMatchQuery(info.InterviewSearch.CompanyName, "company_name")
		es = es.Query(companyQuery)
	}
	if info.TagContent != "" {
		tagQuery = elastic.NewMultiMatchQuery(info.InterviewSearch.TagContent, "tag_content")
		es = es.Query(tagQuery)
	}

	res, err := es.Sort("priority", true).From(offset).Size(limit).Pretty(true).Do(context.Background())
	if err != nil {
		return
	}
	//"founder" : null,
	//"id" : 22,
	//"company_name" : null,
	//"level3_name" : "Redis",
	//"priority" : 1,
	//"tag_content" : null,
	//"level1_name" : "Go",
	//"answer" : null,
	//"interview_title" : "go 字符串",
	//"created_at" : "2023-12-05T01:18:39.000Z",
	//"@timestamp" : "2023-12-05T02:21:00.785Z",
	//"@version" : "1",
	//"level2_name" : "Mysql"
	var interviewSearchss []interviewSearch.InterviewSearchRes
	if res.Hits.TotalHits.Value > 0 {
		for _, v := range res.Hits.Hits {
			fmt.Println(string(v.Source))
			var interviewSearchs interviewSearch.InterviewSearchRes
			json.Unmarshal(v.Source, &interviewSearchs)
			interviewSearchss = append(interviewSearchss, interviewSearchs)
		}
	}
	fmt.Println(interviewSearchss)
	return interviewSearchss, res.Hits.TotalHits.Value, err

}
func (interviewSearchsApi *InterviewSearchApi) GetInterviewSearchList(c *gin.Context) {
	var pageInfo interviewSearchReq.InterviewSearchSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, total, err := InterviewSearch(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
